<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>事件委托</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        html, body{
            width: 100%;
            height: 100%;
            background: white;
            overflow: hidden;
        }
        .btn{
            float: left;
            margin: 0;
            padding: 0;
            width: 100px;
            height: 50px;
            font-size: 16px;
            line-height: 50px;
            text-align: center;
            color: #333333;
        }
        .itemDiv{
            display: none;
            position: absolute;
            top: 50px;
            left: 0;
            width: 300px;
            height: 300px;
            line-height: 300px;
            text-align: center;
            background-color: #cccccc;
            font-size: 24px;
            font-weight: bold;
        }
    </style>
</head>
<body>
<div id="outer">
    <div>
        <button flagIndex=0 class="btn">A</button>
        <button flagIndex=1 class="btn">B</button>
        <button flagIndex=2 class="btn">C</button>
    </div>
    <div class="itemDiv">A</div>
    <div class="itemDiv">B</div>
    <div class="itemDiv">C</div>
</div>
</body>
<script>
    let btns = document.getElementsByClassName("btn");
    let itemDivs = document.getElementsByClassName("itemDiv");


    function itemDivsNode() {
        for (let i=0;i<itemDivs.length;i++){
            itemDivs[i].style.display = "none"
        }
    }

    //普通选项卡
    for (let i = 0; i < btns.length; i++) {
        btns[i].onclick = function () {
            itemDivsNode();
            itemDivs[i].style.display = "block"
        }
    }


    //闭包
    /*    for (var i=0;i<btns.length;i++){
         btns[i].onclick = (function () {
             var a = i;
             return  function () {
                 itemDivsNode();
                 console.log(a);
                 itemDivs[a].style.display = "block"
             }
         })()
     }*/



    //事件委托的形式实现选项卡
/*    let outer = document.getElementById("outer");
    outer.addEventListener("click",function (e) {
        console.log("outer-->event-->",e.target);
//        e.target ---> 触发CLICK事件的元素
        let curBtn = e.target;
        let indexId = curBtn.getAttribute("flagIndex");
        console.log("indexId",indexId);
        itemDivsNode();
        itemDivs[indexId].style.display = "block"
    },false);*/


    console.log(btns);
    console.log(itemDivs);

    //自执行函数
/*    (function a() {
        alert("a")
    })();
    ~function a(b) {
        alert(b)
    }(100);
    */

</script>
</html>